home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl70b2.lha / tcl7.0b2 / tests / expr.test < prev    next >
Text File  |  1993-07-12  |  31KB  |  786 lines

  1. # Commands covered:  expr
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # All rights reserved.
  9. #
  10. # Permission is hereby granted, without written agreement and without
  11. # license or royalty fees, to use, copy, modify, and distribute this
  12. # software and its documentation for any purpose, provided that the
  13. # above copyright notice and the following two paragraphs appear in
  14. # all copies of this software.
  15. #
  16. # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  17. # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  18. # OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  19. # CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. #
  21. # THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  22. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23. # AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  24. # ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  25. # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  26. #
  27. # $Header: /user6/ouster/tcl/tests/RCS/expr.test,v 1.23 93/07/12 11:34:55 ouster Exp $ (Berkeley)
  28.  
  29. if {[string compare test [info procs test]] == 1} then {source defs}
  30.  
  31. # First, test all of the integer operators individually.
  32.  
  33. test expr-1.1 {integer operators} {expr -4} -4
  34. test expr-1.2 {integer operators} {expr -(1+4)} -5
  35. test expr-1.3 {integer operators} {expr ~3} -4
  36. test expr-1.4 {integer operators} {expr !2} 0
  37. test expr-1.5 {integer operators} {expr !0} 1
  38. test expr-1.6 {integer operators} {expr 4*6} 24
  39. test expr-1.7 {integer operators} {expr 36/12} 3
  40. test expr-1.8 {integer operators} {expr 27/4} 6
  41. test expr-1.9 {integer operators} {expr 27%4} 3
  42. test expr-1.10 {integer operators} {expr 2+2} 4
  43. test expr-1.11 {integer operators} {expr 2-6} -4
  44. test expr-1.12 {integer operators} {expr 1<<3} 8
  45. test expr-1.13 {integer operators} {expr 0xff>>2} 63
  46. test expr-1.14 {integer operators} {expr -1>>2} -1
  47. test expr-1.15 {integer operators} {expr 3>2} 1
  48. test expr-1.16 {integer operators} {expr 2>2} 0
  49. test expr-1.17 {integer operators} {expr 1>2} 0
  50. test expr-1.18 {integer operators} {expr 3<2} 0
  51. test expr-1.19 {integer operators} {expr 2<2} 0
  52. test expr-1.20 {integer operators} {expr 1<2} 1
  53. test expr-1.21 {integer operators} {expr 3>=2} 1
  54. test expr-1.22 {integer operators} {expr 2>=2} 1
  55. test expr-1.23 {integer operators} {expr 1>=2} 0
  56. test expr-1.24 {integer operators} {expr 3<=2} 0
  57. test expr-1.25 {integer operators} {expr 2<=2} 1
  58. test expr-1.26 {integer operators} {expr 1<=2} 1
  59. test expr-1.27 {integer operators} {expr 3==2} 0
  60. test expr-1.28 {integer operators} {expr 2==2} 1
  61. test expr-1.29 {integer operators} {expr 3!=2} 1
  62. test expr-1.30 {integer operators} {expr 2!=2} 0
  63. test expr-1.31 {integer operators} {expr 7&0x13} 3
  64. test expr-1.32 {integer operators} {expr 7^0x13} 20
  65. test expr-1.33 {integer operators} {expr 7|0x13} 23
  66. test expr-1.34 {integer operators} {expr 0&&1} 0
  67. test expr-1.35 {integer operators} {expr 0&&0} 0
  68. test expr-1.36 {integer operators} {expr 1&&3} 1
  69. test expr-1.37 {integer operators} {expr 0||1} 1
  70. test expr-1.38 {integer operators} {expr 3||0} 1
  71. test expr-1.39 {integer operators} {expr 0||0} 0
  72. test expr-1.40 {integer operators} {expr 3>2?44:66} 44
  73. test expr-1.41 {integer operators} {expr 2>3?44:66} 66
  74.  
  75. # Check the floating-point operators individually, along with
  76. # automatic conversion to integers where needed.
  77.  
  78. test expr-2.1 {floating-point operators} {expr -4.2} -4.2
  79. test expr-2.2 {floating-point operators} {expr -(1.1+4.2)} -5.3
  80. test expr-2.3 {floating-point operators} {expr !2.1} 0
  81. test expr-2.4 {floating-point operators} {expr !0.0} 1
  82. test expr-2.5 {floating-point operators} {expr 4.2*6.3} 26.46
  83. test expr-2.6 {floating-point operators} {expr 36.0/12.0} 3.0
  84. test expr-2.7 {floating-point operators} {expr 27/4.0} 6.75
  85. test expr-2.8 {floating-point operators} {expr 2.3+2.1} 4.4
  86. test expr-2.9 {floating-point operators} {expr 2.3-6.5} -4.2
  87. test expr-2.10 {floating-point operators} {expr 3.1>2.1} 1
  88. test expr-2.11 {floating-point operators} {expr {2.1 > 2.1}} 0
  89. test expr-2.12 {floating-point operators} {expr 1.23>2.34e+1} 0
  90. test expr-2.13 {floating-point operators} {expr 3.45<2.34} 0
  91. test expr-2.14 {floating-point operators} {expr 0.002e3<--200e-2} 0
  92. test expr-2.15 {floating-point operators} {expr 1.1<2.1} 1
  93. test expr-2.16 {floating-point operators} {expr 3.1>=2.2} 1
  94. test expr-2.17 {floating-point operators} {expr 2.345>=2.345} 1
  95. test expr-2.18 {floating-point operators} {expr 1.1>=2.2} 0
  96. test expr-2.19 {floating-point operators} {expr 3.0<=2.0} 0
  97. test expr-2.20 {floating-point operators} {expr 2.2<=2.2} 1
  98. test expr-2.21 {floating-point operators} {expr 2.2<=2.2001} 1
  99. test expr-2.22 {floating-point operators} {expr 3.2==2.2} 0
  100. test expr-2.23 {floating-point operators} {expr 2.2==2.2} 1
  101. test expr-2.24 {floating-point operators} {expr 3.2!=2.2} 1
  102. test expr-2.25 {floating-point operators} {expr 2.2!=2.2} 0
  103. test expr-2.26 {floating-point operators} {expr 0.0&&0.0} 0
  104. test expr-2.27 {floating-point operators} {expr 0.0&&1.3} 0
  105. test expr-2.28 {floating-point operators} {expr 1.3&&0.0} 0
  106. test expr-2.29 {floating-point operators} {expr 1.3&&3.3} 1
  107. test expr-2.30 {floating-point operators} {expr 0.0||0.0} 0
  108. test expr-2.31 {floating-point operators} {expr 0.0||1.3} 1
  109. test expr-2.32 {floating-point operators} {expr 1.3||0.0} 1
  110. test expr-2.33 {floating-point operators} {expr 3.3||0.0} 1
  111. test expr-2.34 {floating-point operators} {expr 3.3>2.3?44.3:66.3} 44.3
  112. test expr-2.35 {floating-point operators} {expr 2.3>3.3?44.3:66.3} 66.3
  113.  
  114. # Operators that aren't legal on floating-point numbers
  115.  
  116. test expr-3.1 {illegal floating-point operations} {
  117.     list [catch {expr ~4.0} msg] $msg
  118. } {1 {can't use floating-point value as operand of "~"}}
  119. test expr-3.2 {illegal floating-point operations} {
  120.     list [catch {expr 27%4.0} msg] $msg
  121. } {1 {can't use floating-point value as operand of "%"}}
  122. test expr-3.3 {illegal floating-point operations} {
  123.     list [catch {expr 27.0%4} msg] $msg
  124. } {1 {can't use floating-point value as operand of "%"}}
  125. test expr-3.4 {illegal floating-point operations} {
  126.     list [catch {expr 1.0<<3} msg] $msg
  127. } {1 {can't use floating-point value as operand of "<<"}}
  128. test expr-3.5 {illegal floating-point operations} {
  129.     list [catch {expr 3<<1.0} msg] $msg
  130. } {1 {can't use floating-point value as operand of "<<"}}
  131. test expr-3.6 {illegal floating-point operations} {
  132.     list [catch {expr 24.0>>3} msg] $msg
  133. } {1 {can't use floating-point value as operand of ">>"}}
  134. test expr-3.7 {illegal floating-point operations} {
  135.     list [catch {expr 24>>3.0} msg] $msg
  136. } {1 {can't use floating-point value as operand of ">>"}}
  137. test expr-3.8 {illegal floating-point operations} {
  138.     list [catch {expr 24&3.0} msg] $msg
  139. } {1 {can't use floating-point value as operand of "&"}}
  140. test expr-3.9 {illegal floating-point operations} {
  141.     list [catch {expr 24.0|3} msg] $msg
  142. } {1 {can't use floating-point value as operand of "|"}}
  143. test expr-3.10 {illegal floating-point operations} {
  144.     list [catch {expr 24.0^3} msg] $msg
  145. } {1 {can't use floating-point value as operand of "^"}}
  146.  
  147. # Check the string operators individually.
  148.  
  149. test expr-4.1 {string operators} {expr {"abc" > "def"}} 0
  150. test expr-4.2 {string operators} {expr {"def" > "def"}} 0
  151. test expr-4.3 {string operators} {expr {"g" > "def"}} 1
  152. test expr-4.4 {string operators} {expr {"abc" < "abd"}} 1
  153. test expr-4.5 {string operators} {expr {"abd" < "abd"}} 0
  154. test expr-4.6 {string operators} {expr {"abe" < "abd"}} 0
  155. test expr-4.7 {string operators} {expr {"abc" >= "def"}} 0
  156. test expr-4.8 {string operators} {expr {"def" >= "def"}} 1
  157. test expr-4.9 {string operators} {expr {"g" >= "def"}} 1
  158. test expr-4.10 {string operators} {expr {"abc" <= "abd"}} 1
  159. test expr-4.11 {string operators} {expr {"abd" <= "abd"}} 1
  160. test expr-4.12 {string operators} {expr {"abe" <= "abd"}} 0
  161. test expr-4.13 {string operators} {expr {"abc" == "abd"}} 0
  162. test expr-4.14 {string operators} {expr {"abd" == "abd"}} 1
  163. test expr-4.15 {string operators} {expr {"abc" != "abd"}} 1
  164. test expr-4.16 {string operators} {expr {"abd" != "abd"}} 0
  165. test expr-4.17 {string operators} {expr {"0y" < "0x12"}} 1
  166. test expr-4.18 {string operators} {expr {1?"foo":"bar"}} foo
  167. test expr-4.19 {string operators} {expr {0?"foo":"bar"}} bar
  168.  
  169. # Operators that aren't legal on string operands.
  170.  
  171. test expr-5.1 {illegal string operations} {
  172.     list [catch {expr {-"a"}} msg] $msg
  173. } {1 {can't use non-numeric string as operand of "-"}}
  174. test expr-5.2 {illegal string operations} {
  175.     list [catch {expr {~"a"}} msg] $msg
  176. } {1 {can't use non-numeric string as operand of "~"}}
  177. test expr-5.3 {illegal string operations} {
  178.     list [catch {expr {!"a"}} msg] $msg
  179. } {1 {can't use non-numeric string as operand of "!"}}
  180. test expr-5.4 {illegal string operations} {
  181.     list [catch {expr {"a"*"b"}} msg] $msg
  182. } {1 {can't use non-numeric string as operand of "*"}}
  183. test expr-5.5 {illegal string operations} {
  184.     list [catch {expr {"a"/"b"}} msg] $msg
  185. } {1 {can't use non-numeric string as operand of "/"}}
  186. test expr-5.6 {illegal string operations} {
  187.     list [catch {expr {"a"%"b"}} msg] $msg
  188. } {1 {can't use non-numeric string as operand of "%"}}
  189. test expr-5.7 {illegal string operations} {
  190.     list [catch {expr {"a"+"b"}} msg] $msg
  191. } {1 {can't use non-numeric string as operand of "+"}}
  192. test expr-5.8 {illegal string operations} {
  193.     list [catch {expr {"a"-"b"}} msg] $msg
  194. } {1 {can't use non-numeric string as operand of "-"}}
  195. test expr-5.9 {illegal string operations} {
  196.     list [catch {expr {"a"<<"b"}} msg] $msg
  197. } {1 {can't use non-numeric string as operand of "<<"}}
  198. test expr-5.10 {illegal string operations} {
  199.     list [catch {expr {"a">>"b"}} msg] $msg
  200. } {1 {can't use non-numeric string as operand of ">>"}}
  201. test expr-5.11 {illegal string operations} {
  202.     list [catch {expr {"a"&"b"}} msg] $msg
  203. } {1 {can't use non-numeric string as operand of "&"}}
  204. test expr-5.12 {illegal string operations} {
  205.     list [catch {expr {"a"^"b"}} msg] $msg
  206. } {1 {can't use non-numeric string as operand of "^"}}
  207. test expr-5.13 {illegal string operations} {
  208.     list [catch {expr {"a"|"b"}} msg] $msg
  209. } {1 {can't use non-numeric string as operand of "|"}}
  210. test expr-5.14 {illegal string operations} {
  211.     list [catch {expr {"a"&&"b"}} msg] $msg
  212. } {1 {can't use non-numeric string as operand of "&&"}}
  213. test expr-5.15 {illegal string operations} {
  214.     list [catch {expr {"a"||"b"}} msg] $msg
  215. } {1 {can't use non-numeric string as operand of "||"}}
  216. test expr-5.16 {illegal string operations} {
  217.     list [catch {expr {"a"?4:2}} msg] $msg
  218. } {1 {can't use non-numeric string as operand of "?"}}
  219.  
  220. # Check precedence pairwise.
  221.  
  222. test expr-6.1 {precedence checks} {expr -~3} 4
  223. test expr-6.2 {precedence checks} {expr -!3} 0
  224. test expr-6.3 {precedence checks} {expr -~0} 1
  225.  
  226. test expr-7.1 {precedence checks} {expr 2*4/6} 1
  227. test expr-7.2 {precedence checks} {expr 24/6*3} 12
  228. test expr-7.3 {precedence checks} {expr 24/6/2} 2
  229.  
  230. test expr-8.1 {precedence checks} {expr -2+4} 2
  231. test expr-8.2 {precedence checks} {expr -2-4} -6
  232.  
  233. test expr-9.1 {precedence checks} {expr 2*3+4} 10
  234. test expr-9.2 {precedence checks} {expr 8/2+4} 8
  235. test expr-9.3 {precedence checks} {expr 8%3+4} 6
  236. test expr-9.4 {precedence checks} {expr 2*3-1} 5
  237. test expr-9.5 {precedence checks} {expr 8/2-1} 3
  238. test expr-9.6 {precedence checks} {expr 8%3-1} 1
  239.  
  240. test expr-10.1 {precedence checks} {expr 6-3-2} 1
  241.  
  242. test expr-11.1 {precedence checks} {expr 7+1>>2} 2
  243. test expr-11.2 {precedence checks} {expr 7+1<<2} 32
  244. test expr-11.3 {precedence checks} {expr 7>>3-2} 3
  245. test expr-11.4 {precedence checks} {expr 7<<3-2} 14
  246.  
  247. test expr-12.1 {precedence checks} {expr 6>>1>4} 0
  248. test expr-12.2 {precedence checks} {expr 6>>1<2} 0
  249. test expr-12.3 {precedence checks} {expr 6>>1>=3} 1
  250. test expr-12.4 {precedence checks} {expr 6>>1<=2} 0
  251. test expr-12.5 {precedence checks} {expr 6<<1>5} 1
  252. test expr-12.6 {precedence checks} {expr 6<<1<5} 0
  253. test expr-12.7 {precedence checks} {expr 5<=6<<1} 1
  254. test expr-12.8 {precedence checks} {expr 5>=6<<1} 0
  255.  
  256. test expr-13.1 {precedence checks} {expr 2<3<4} 1
  257. test expr-13.2 {precedence checks} {expr 0<4>2} 0
  258. test expr-13.3 {precedence checks} {expr 4>2<1} 0
  259. test expr-13.4 {precedence checks} {expr 4>3>2} 0
  260. test expr-13.5 {precedence checks} {expr 4>3>=2} 0
  261. test expr-13.6 {precedence checks} {expr 4>=3>2} 0
  262. test expr-13.7 {precedence checks} {expr 4>=3>=2} 0
  263. test expr-13.8 {precedence checks} {expr 0<=4>=2} 0
  264. test expr-13.9 {precedence checks} {expr 4>=2<=0} 0
  265. test expr-10.10 {precedence checks} {expr 2<=3<=4} 1
  266.  
  267. test expr-14.1 {precedence checks} {expr 1==4>3} 1
  268. test expr-14.2 {precedence checks} {expr 0!=4>3} 1
  269. test expr-14.3 {precedence checks} {expr 1==3<4} 1
  270. test expr-14.4 {precedence checks} {expr 0!=3<4} 1
  271. test expr-14.5 {precedence checks} {expr 1==4>=3} 1
  272. test expr-14.6 {precedence checks} {expr 0!=4>=3} 1
  273. test expr-14.7 {precedence checks} {expr 1==3<=4} 1
  274. test expr-14.8 {precedence checks} {expr 0!=3<=4} 1
  275.  
  276. test expr-15.1 {precedence checks} {expr 1==3==3} 0
  277. test expr-15.2 {precedence checks} {expr 3==3!=2} 1
  278. test expr-15.3 {precedence checks} {expr 2!=3==3} 0
  279. test expr-15.4 {precedence checks} {expr 2!=1!=1} 0
  280.  
  281. test expr-16.1 {precedence checks} {expr 2&3==2} 0
  282. test expr-16.2 {precedence checks} {expr 1&3!=3} 0
  283.  
  284. test expr-17.1 {precedence checks} {expr 7&3^0x10} 19
  285. test expr-17.2 {precedence checks} {expr 7^0x10&3} 7
  286.  
  287. test expr-18.1 {precedence checks} {expr 7^0x10|3} 23
  288. test expr-18.2 {precedence checks} {expr 7|0x10^3} 23
  289.  
  290. test expr-19.1 {precedence checks} {expr 7|3&&1} 1
  291. test expr-19.2 {precedence checks} {expr 1&&3|7} 1
  292. test expr-19.3 {precedence checks} {expr 0&&1||1} 1
  293. test expr-19.4 {precedence checks} {expr 1||1&&0} 1
  294.  
  295. test expr-20.1 {precedence checks} {expr 1||0?3:4} 3
  296. test expr-20.2 {precedence checks} {expr 1?0:4||1} 0
  297.  
  298. # Parentheses.
  299.  
  300. test expr-21.1 {parenthesization} {expr (2+4)*6} 36
  301. test expr-21.2 {parenthesization} {expr (1?0:4)||1} 1
  302.  
  303. # Embedded commands and variable names.
  304.  
  305. set a 16
  306. test expr-22.1 {embedded variables} {expr {2*$a}} 32
  307. test expr-22.2 {embedded variables} {
  308.     set x -5
  309.     set y 10
  310.     expr {$x + $y}
  311. } {5}
  312. test expr-22.3 {embedded variables} {
  313.     set x "  -5"
  314.     set y "  +10"
  315.     expr {$x + $y}
  316. } {5}
  317. test expr-22.4 {embedded commands and variables} {expr {[set a] - 14}} 2
  318. test expr-22.5 {embedded commands and variables} {
  319.     list [catch {expr {12 - [bad_command_name]}} msg] $msg
  320. } {1 {invalid command name: "bad_command_name"}}
  321.  
  322. # Double-quotes and things inside them.
  323.  
  324. test expr-23.1 {double-quotes} {expr {"abc"}} abc
  325. test expr-23.2 {double-quotes} {
  326.     set a 189
  327.     expr {"$a.bc"}
  328. } 189.bc
  329. test expr-23.3 {double-quotes} {
  330.     set b2 xyx
  331.     expr {"$b2$b2$b2.[set b2].[set b2]"}
  332. } xyxxyxxyx.xyx.xyx
  333. test expr-23.4 {double-quotes} {expr {"11\}\}22"}} 11}}22
  334. test expr-23.5 {double-quotes} {expr {"\*bc"}} {*bc}
  335. test expr-23.6 {double-quotes} {
  336.     catch {unset bogus__}
  337.     list [catch {expr {"$bogus__"}} msg] $msg
  338. } {1 {can't read "bogus__": no such variable}}
  339. test expr-23.7 {double-quotes} {
  340.     list [catch {expr {"a[error Testing]bc"}} msg] $msg
  341. } {1 Testing}
  342.  
  343. # Numbers in various bases.
  344.  
  345. test expr-24.1 {numbers in different bases} {expr 0x20} 32
  346. test expr-24.2 {numbers in different bases} {expr 015} 13
  347.  
  348. # Conversions between various data types.
  349.  
  350. test expr-25.1 {type conversions} {expr 2+2.5} 4.5
  351. test expr-25.2 {type conversions} {expr 2.5+2} 4.5
  352. test expr-25.3 {type conversions} {expr 2-2.5} -0.5
  353. test expr-25.4 {type conversions} {expr 2/2.5} 0.8
  354. test expr-25.5 {type conversions} {expr 2>2.5} 0
  355. test expr-25.6 {type conversions} {expr 2.5>2} 1
  356. test expr-25.7 {type conversions} {expr 2<2.5} 1
  357. test expr-25.8 {type conversions} {expr 2>=2.5} 0
  358. test expr-25.9 {type conversions} {expr 2<=2.5} 1
  359. test expr-25.10 {type conversions} {expr 2==2.5} 0
  360. test expr-25.11 {type conversions} {expr 2!=2.5} 1
  361. test expr-25.12 {type conversions} {expr 2>"ab"} 0
  362. test expr-25.13 {type conversions} {expr {2>" "}} 1
  363. test expr-25.14 {type conversions} {expr {"24.1a" > 24.1}} 1
  364. test expr-25.15 {type conversions} {expr {24.1 > "24.1a"}} 0
  365. test expr-25.16 {type conversions} {expr 2+2.5} 4.5
  366. test expr-25.17 {type conversions} {expr 2+2.5} 4.5
  367. test expr-25.18 {type conversions} {expr 2.0e2} 200.0
  368. test expr-25.19 {type conversions} {expr 2.0e15} 2e+15
  369. test expr-25.20 {type conversions} {expr 10.0} 10.0
  370.  
  371. # Various error conditions.
  372.  
  373. test expr-26.1 {error conditions} {
  374.     list [catch {expr 2+"a"} msg] $msg
  375. } {1 {can't use non-numeric string as operand of "+"}}
  376. test expr-26.2 {error conditions} {
  377.     list [catch {expr 2+4*} msg] $msg
  378. } {1 {syntax error in expression "2+4*"}}
  379. test expr-26.3 {error conditions} {
  380.     list [catch {expr 2+4*(} msg] $msg
  381. } {1 {syntax error in expression "2+4*("}}
  382. catch {unset _non_existent_}
  383. test expr-26.4 {error conditions} {
  384.     list [catch {expr 2+$_non_existent_} msg] $msg
  385. } {1 {can't read "_non_existent_": no such variable}}
  386. set a xx
  387. test expr-26.5 {error conditions} {
  388.     list [catch {expr {2+$a}} msg] $msg
  389. } {1 {can't use non-numeric string as operand of "+"}}
  390. test expr-26.6 {error conditions} {
  391.     list [catch {expr {2+[set a]}} msg] $msg
  392. } {1 {can't use non-numeric string as operand of "+"}}
  393. test expr-26.7 {error conditions} {
  394.     list [catch {expr {2+(4}} msg] $msg
  395. } {1 {unmatched parentheses in expression "2+(4"}}
  396. test expr-26.8 {error conditions} {
  397.     list [catch {expr 2/0} msg] $msg $errorCode
  398. } {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
  399. test expr-26.9 {error conditions} {
  400.     list [catch {expr 2%0} msg] $msg $errorCode
  401. } {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
  402. test expr-26.10 {error conditions} {
  403.     list [catch {expr 2.0/0.0} msg] $msg $errorCode
  404. } {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
  405. test expr-26.11 {error conditions} {
  406.     list [catch {expr 2#} msg] $msg
  407. } {1 {syntax error in expression "2#"}}
  408. test expr-26.12 {error conditions} {
  409.     list [catch {expr a.b} msg] $msg
  410. } {1 {syntax error in expression "a.b"}}
  411. test expr-26.13 {error conditions} {
  412.     list [catch {expr {"a"/"b"}} msg] $msg
  413. } {1 {can't use non-numeric string as operand of "/"}}
  414. test expr-26.14 {error conditions} {
  415.     list [catch {expr 2:3} msg] $msg
  416. } {1 {can't have : operator without ? first}}
  417. test expr-26.15 {error conditions} {
  418.     list [catch {expr a@b} msg] $msg
  419. } {1 {syntax error in expression "a@b"}}
  420. test expr-26.16 {error conditions} {
  421.     list [catch {expr a[b} msg] $msg
  422. } {1 {missing close-bracket}}
  423. test expr-26.17 {error conditions} {
  424.     list [catch {expr a`b} msg] $msg
  425. } {1 {syntax error in expression "a`b"}}
  426. test expr-26.18 {error conditions} {
  427.     list [catch {expr \"a\"\{b} msg] $msg
  428. } {1 {missing close-brace}}
  429. test expr-26.19 {error conditions} {
  430.     list [catch {expr a} msg] $msg
  431. } {1 {syntax error in expression "a"}}
  432. test expr-26.20 {error conditions} {
  433.     list [catch expr msg] $msg
  434. } {1 {wrong # args: should be "expr arg ?arg ...?"}}
  435.  
  436. # Cancelled evaluation.
  437.  
  438. test expr-27.1 {cancelled evaluation} {
  439.     set a 1
  440.     expr {0&&[set a 2]}
  441.     set a
  442. } 1
  443. test expr-27.2 {cancelled evaluation} {
  444.     set a 1
  445.     expr {1||[set a 2]}
  446.     set a
  447. } 1
  448. test expr-27.3 {cancelled evaluation} {
  449.     set a 1
  450.     expr {0?[set a 2]:1}
  451.     set a
  452. } 1
  453. test expr-27.4 {cancelled evaluation} {
  454.     set a 1
  455.     expr {1?2:[set a 2]}
  456.     set a
  457. } 1
  458. catch {unset x}
  459. test expr-27.5 {cancelled evaluation} {
  460.     list [catch {expr {[info exists x] && $x}} msg] $msg
  461. } {0 0}
  462. test expr-27.6 {cancelled evaluation} {
  463.     list [catch {expr {0 && [concat $x]}} msg] $msg
  464. } {0 0}
  465.  
  466. # Tcl_ExprBool as used in "if" statements
  467.  
  468. test expr-28.1 {Tcl_ExprBoolean usage} {
  469.     set a 1
  470.     if {2} {set a 2}
  471.     set a
  472. } 2
  473. test expr-28.2 {Tcl_ExprBoolean usage} {
  474.     set a 1
  475.     if {0} {set a 2}
  476.     set a
  477. } 1
  478. test expr-28.3 {Tcl_ExprBoolean usage} {
  479.     set a 1
  480.     if {1.2} {set a 2}
  481.     set a
  482. } 2
  483. test expr-28.4 {Tcl_ExprBoolean usage} {
  484.     set a 1
  485.     if {-1.1} {set a 2}
  486.     set a
  487. } 2
  488. test expr-28.5 {Tcl_ExprBoolean usage} {
  489.     set a 1
  490.     if {0.0} {set a 2}
  491.     set a
  492. } 1
  493. test expr-28.6 {Tcl_ExprBoolean usage} {
  494.     set a 1
  495.     if {"YES"} {set a 2}
  496.     set a
  497. } 2
  498. test expr-28.7 {Tcl_ExprBoolean usage} {
  499.     set a 1
  500.     if {"no"} {set a 2}
  501.     set a
  502. } 1
  503. test expr-28.8 {Tcl_ExprBoolean usage} {
  504.     set a 1
  505.     if {"true"} {set a 2}
  506.     set a
  507. } 2
  508. test expr-28.9 {Tcl_ExprBoolean usage} {
  509.     set a 1
  510.     if {"fAlse"} {set a 2}
  511.     set a
  512. } 1
  513. test expr-28.10 {Tcl_ExprBoolean usage} {
  514.     set a 1
  515.     if {"on"} {set a 2}
  516.     set a
  517. } 2
  518. test expr-28.11 {Tcl_ExprBoolean usage} {
  519.     set a 1
  520.     if {"Off"} {set a 2}
  521.     set a
  522. } 1
  523. test expr-28.12 {Tcl_ExprBool usage} {
  524.     list [catch {if {"abc"} {}} msg] $msg
  525. } {1 {expected boolean value but got "abc"}}
  526.  
  527. # Operands enclosed in braces
  528.  
  529. test expr-29.1 {braces} {expr {{abc}}} abc
  530. test expr-29.2 {braces} {expr {{00010}}} 8
  531. test expr-29.3 {braces} {expr {{3.1200000}}} 3.12
  532. test expr-29.4 {braces} {expr {{a{b}{1 {2 3}}c}}} "a{b}{1 {2 3}}c"
  533. test expr-29.5 {braces} {
  534.     list [catch {expr "\{abc"} msg] $msg
  535. } {1 {missing close-brace}}
  536.  
  537. # Very long values
  538.  
  539. test expr-30.1 {long values} {
  540.     set a "0000 1111 2222 3333 4444"
  541.     set a "$a | $a | $a | $a | $a"
  542.     set a "$a || $a || $a || $a || $a"
  543.     expr {$a}
  544. } {0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 || 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 || 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 || 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 || 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444}
  545. test expr-30.2 {long values} {
  546.     set a "000000000000000000000000000000"
  547.     set a "$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a${a}5"
  548.     expr $a
  549. } 5
  550.  
  551. # Expressions spanning multiple arguments
  552.  
  553. test expr-31.1 {multiple arguments to expr command} {
  554.     expr 4 + ( 6 *12) -3
  555. } 73
  556. test expr-31.2 {multiple arguments to expr command} {
  557.     list [catch {expr 2 + (3 + 4} msg] $msg
  558. } {1 {unmatched parentheses in expression "2 + (3 + 4"}}
  559. test expr-31.3 {multiple arguments to expr command} {
  560.     list [catch {expr 2 + 3 +} msg] $msg
  561. } {1 {syntax error in expression "2 + 3 +"}}
  562. test expr-31.4 {multiple arguments to expr command} {
  563.     list [catch {expr 2 + 3 )} msg] $msg
  564. } {1 {syntax error in expression "2 + 3 )"}}
  565.  
  566. # Math functions
  567.  
  568. test expr-32.1 {math functions in expressions} {
  569.     expr acos(0.5)
  570. } {1.0472}
  571. test expr-32.2 {math functions in expressions} {
  572.     expr asin(0.5)
  573. } {0.523599}
  574. test expr-32.3 {math functions in expressions} {
  575.     expr atan(1.0)
  576. } {0.785398}
  577. test expr-32.4 {math functions in expressions} {
  578.     expr atan2(2.0, 2.0)
  579. } {0.785398}
  580. test expr-32.5 {math functions in expressions} {
  581.     expr ceil(1.999)
  582. } {2.0}
  583. test expr-32.6 {math functions in expressions} {
  584.     expr cos(.1)
  585. } {0.995004}
  586. test expr-32.7 {math functions in expressions} {
  587.     expr cosh(.1)
  588. } {1.005}
  589. test expr-32.8 {math functions in expressions} {
  590.     expr exp(1.0)
  591. } {2.71828}
  592. test expr-32.9 {math functions in expressions} {
  593.     expr floor(2.000)
  594. } {2.0}
  595. test expr-32.10 {math functions in expressions} {
  596.     expr floor(2.001)
  597. } {2.0}
  598. test expr-32.11 {math functions in expressions} {
  599.     expr fmod(7.3, 3.2)
  600. } {0.9}
  601. test expr-32.12 {math functions in expressions} {
  602.     expr hypot(3.0, 4.0)
  603. } {5.0}
  604. test expr-32.13 {math functions in expressions} {
  605.     expr log(2.8)
  606. } {1.02962}
  607. test expr-32.14 {math functions in expressions} {
  608.     expr log10(2.8)
  609. } {0.447158}
  610. test expr-32.15 {math functions in expressions} {
  611.     expr pow(2.1, 3.1)
  612. } {9.97424}
  613. test expr-32.16 {math functions in expressions} {
  614.     expr sin(.1)
  615. } {0.0998334}
  616. test expr-32.17 {math functions in expressions} {
  617.     expr sinh(.1)
  618. } {0.100167}
  619. test expr-32.18 {math functions in expressions} {
  620.     expr sqrt(2.0)
  621. } {1.41421}
  622. test expr-32.19 {math functions in expressions} {
  623.     expr tan(0.8)
  624. } {1.02964}
  625. test expr-32.20 {math functions in expressions} {
  626.     expr tanh(0.8)
  627. } {0.664037}
  628. test expr-32.21 {math functions in expressions} {
  629.     expr abs(-1.8)
  630. } {1.8}
  631. test expr-32.22 {math functions in expressions} {
  632.     expr abs(10.0)
  633. } {10.0}
  634. test expr-32.23 {math functions in expressions} {
  635.     expr abs(-4)
  636. } {4}
  637. test expr-32.24 {math functions in expressions} {
  638.     expr abs(66)
  639. } {66}
  640. if ($atBerkeley) {
  641.     test expr-32.25 {math functions in expressions} {
  642.     list [catch {expr abs(0x80000000)} msg] $msg
  643.     } {1 {integer value too large to represent}}
  644. }
  645. test expr-32.26 {math functions in expressions} {
  646.     expr double(1)
  647. } {1.0}
  648. test expr-32.27 {math functions in expressions} {
  649.     expr double(1.1)
  650. } {1.1}
  651. test expr-32.28 {math functions in expressions} {
  652.     expr int(1)
  653. } {1}
  654. test expr-32.29 {math functions in expressions} {
  655.     expr int(1.4)
  656. } {1}
  657. test expr-32.30 {math functions in expressions} {
  658.     expr int(1.6)
  659. } {1}
  660. test expr-32.31 {math functions in expressions} {
  661.     expr int(-1.4)
  662. } {-1}
  663. test expr-32.32 {math functions in expressions} {
  664.     expr int(-1.6)
  665. } {-1}
  666. test expr-32.33 {math functions in expressions} {
  667.     list [catch {expr int(1e60)} msg] $msg
  668. } {1 {integer value too large to represent}}
  669. test expr-32.34 {math functions in expressions} {
  670.     list [catch {expr int(-1e60)} msg] $msg
  671. } {1 {integer value too large to represent}}
  672. test expr-32.35 {math functions in expressions} {
  673.     expr round(1.49)
  674. } {1}
  675. test expr-32.36 {math functions in expressions} {
  676.     expr round(1.51)
  677. } {2}
  678. test expr-32.37 {math functions in expressions} {
  679.     expr round(-1.49)
  680. } {-1}
  681. test expr-32.38 {math functions in expressions} {
  682.     expr round(-1.51)
  683. } {-2}
  684. test expr-32.39 {math functions in expressions} {
  685.     list [catch {expr round(1e60)} msg] $msg
  686. } {1 {integer value too large to represent}}
  687. test expr-32.40 {math functions in expressions} {
  688.     list [catch {expr round(-1e60)} msg] $msg
  689. } {1 {integer value too large to represent}}
  690. test expr-32.41 {math functions in expressions} {
  691.     list [catch {expr pow(1.0 + 3.0 - 2, .8 * 5)} msg] $msg
  692. } {0 16.0}
  693. test expr-32.42 {math functions in expressions} {
  694.     list [catch {expr hypot(5*.8,3)} msg] $msg
  695. } {0 5.0}
  696.  
  697. test expr-33.1 {conversions and fancy args to math functions} {
  698.     expr hypot ( 3 , 4 )
  699. } 5.0
  700. test expr-33.2 {conversions and fancy args to math functions} {
  701.     expr hypot ( (2.0+1.0) , 4 )
  702. } 5.0
  703. test expr-33.3 {conversions and fancy args to math functions} {
  704.     expr hypot ( 3 , (3.0 + 1.0) )
  705. } 5.0
  706. test expr-33.4 {conversions and fancy args to math functions} {
  707.     expr cos(acos(0.1))
  708. } 0.1
  709.  
  710. test expr-34.1 {errors in math functions} {
  711.     list [catch {expr func_2(1.0)} msg] $msg
  712. } {1 {unknown math function "func_2"}}
  713. test expr-34.2 {errors in math functions} {
  714.     list [catch {expr func|(1.0)} msg] $msg
  715. } {1 {syntax error in expression "func|(1.0)"}}
  716. test expr-34.3 {errors in math functions} {
  717.     list [catch {expr {hypot("a b", 2.0)}} msg] $msg
  718. } {1 {argument to math function didn't have numeric value}}
  719. test expr-34.4 {errors in math functions} {
  720.     list [catch {expr hypot(1.0 2.0)} msg] $msg
  721. } {1 {syntax error in expression "hypot(1.0 2.0)"}}
  722. test expr-34.5 {errors in math functions} {
  723.     list [catch {expr hypot(1.0, 2.0} msg] $msg
  724. } {1 {syntax error in expression "hypot(1.0, 2.0"}}
  725. test expr-34.6 {errors in math functions} {
  726.     list [catch {expr hypot(1.0 ,} msg] $msg
  727. } {1 {syntax error in expression "hypot(1.0 ,"}}
  728. test expr-34.7 {errors in math functions} {
  729.     list [catch {expr hypot(1.0)} msg] $msg
  730. } {1 {too few arguments for math function}}
  731. test expr-34.8 {errors in math functions} {
  732.     list [catch {expr hypot(1.0, 2.0, 3.0)} msg] $msg
  733. } {1 {too many arguments for math function}}
  734. test expr-34.9 {errors in math functions} {
  735.     list [catch {expr acos(-2.0)} msg] $msg $errorCode
  736. } {1 {domain error: argument not in valid range} {ARITH DOMAIN {domain error: argument not in valid range}}}
  737. test expr-34.10 {errors in math functions} {
  738.     list [catch {expr pow(-3, 1000001)} msg] $msg $errorCode
  739. } {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}}
  740. test expr-34.11 {errors in math functions} {
  741.     list [catch {expr pow(3, 1000001)} msg] $msg $errorCode
  742. } {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}}
  743. test expr-34.12 {errors in math functions} {
  744.     list [catch {expr -14.0*exp(100000)} msg] $msg $errorCode
  745. } {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}}
  746. test expr-34.13 {errors in math functions} {
  747.     list [catch {expr int(1.0e30)} msg] $msg $errorCode
  748. } {1 {integer value too large to represent} {ARITH IOVERFLOW {integer value too large to represent}}}
  749. test expr-34.14 {errors in math functions} {
  750.     list [catch {expr int(-1.0e30)} msg] $msg $errorCode
  751. } {1 {integer value too large to represent} {ARITH IOVERFLOW {integer value too large to represent}}}
  752. test expr-34.15 {errors in math functions} {
  753.     list [catch {expr round(1.0e30)} msg] $msg $errorCode
  754. } {1 {integer value too large to represent} {ARITH IOVERFLOW {integer value too large to represent}}}
  755. test expr-34.16 {errors in math functions} {
  756.     list [catch {expr round(-1.0e30)} msg] $msg $errorCode
  757. } {1 {integer value too large to represent} {ARITH IOVERFLOW {integer value too large to represent}}}
  758.  
  759. catch {unset tcl_precision}
  760. test expr-35.1 {tcl_precision variable} {
  761.     expr 2.0/3
  762. } 0.666667
  763. set tcl_precision 1
  764. test expr-35.2 {tcl_precision variable} {
  765.     expr 2.0/3
  766. } 0.7
  767. test expr-35.3 {tcl_precision variable} {
  768.     expr 2.0/3
  769. } 0.7
  770. test expr-35.4 {tcl_precision variable} {
  771.     list [catch {set tcl_precision 0} msg] $msg [expr 2.0/3]
  772. } {1 {can't set "tcl_precision": improper value for precision} 0.7}
  773. test expr-35.5 {tcl_precision variable} {
  774.     list [catch {set tcl_precision 101} msg] $msg [expr 2.0/3]
  775. } {1 {can't set "tcl_precision": improper value for precision} 0.7}
  776. test expr-35.6 {tcl_precision variable} {
  777.     list [catch {set tcl_precision {}} msg] $msg [expr 2.0/3]
  778. } {1 {can't set "tcl_precision": improper value for precision} 0.7}
  779. test expr-35.7 {tcl_precision variable} {
  780.     list [catch {set tcl_precision {1 2 3}} msg] $msg [expr 2.0/3]
  781. } {1 {can't set "tcl_precision": improper value for precision} 0.7}
  782. catch {unset tcl_precision}
  783. test expr-35.8 {tcl_precision variable} {
  784.     expr 2.0/3
  785. } 0.666667
  786.